#pragma once
#include <fstream>
#include <unordered_map>
#include "log.hpp"
Log lg;

const std::string dictname = "./dict.txt";
const std::string sep = ":";

static bool Split(std::string &line, std::string *part1, std::string *part2)
{
  auto pos = line.find(sep);
  if (pos == std::string::npos)
    return false;
  *part1 = line.substr(0, pos);
  *part2 = line.substr(pos + 1);
}

class Init
{
public:
  Init()
  {
    std::ifstream in(dictname);
    if (!in.is_open())
    {
      lg(Fatal, "ifstream open failed, errno: %d, errmsg: %s", errno, strerror(errno));
      exit(1);
    }

    std::string line;
    while (std::getline(in, line))
    {
      std::string part1, part2;
      Split(line, &part1, &part2);
      _dict.insert({part1, part2});
    }
    in.close();
  }

  std::string translation(const std::string &english)
  {
    auto find = _dict.find(english);
    if (find == _dict.end())
    {
      return "Unknow\n";
    }
    else
      return find->second;
  }

private:
  std::unordered_map<std::string, std::string> _dict;
};